<?php
// Russell RSS <version>
// Russell's original source code is (c) David Kendal
//---------------------------------------------------
// This work 'as-is' we provide.
// No warranty express or implied.
// We've done our best,
// to debug and test.
// Liability for damages denied.
//
// Permission is granted hereby,
// to copy, share, and modify.
// Use as is fit,
// free or for profit.
// These rights, on this notice, rely.
//---------------------------------------------------


class RSS {
  // info wairiables.
  private $title;        // title of the feed
  private $link;         // link to the site this feed references
  private $description;  // description of the site
  private $language;     // language; defaults to en-uk
  private $author;       // author, format as array('name' => 'John Smith', 'email' => 'john@smith.co.uk')
  private $editor;       // editor, format as author
  private $webmaster;    // webmaster, as author
  
  public $entries;       // an array of all the entries
  private $entrynr;      // the current position of the entery pointer.
                         // when the feed is rendered, this is used as the total value
  
  public $rendered;      // contains the code of the most recently rendered feed
  
  // constructor. does very little.
  public function __construct () {
    $this->language = "en-uk";
    $this->entrynr = 0;
    return TRUE;
  }
  
  // set meta information for this feed.
  // takes an array of the values below.
  public function set_feed_info ($info) {
    // in order to keep this concise, each case is all on one line.
    // it should be easy enough to read.
    if (isset($info['title']))       { $this->title       = $info['title'];       }
    if (isset($info['link']))        { $this->link        = $info['link'];        }
    if (isset($info['description'])) { $this->description = $info['description']; }
    if (isset($info['language']))    { $this->language    = $info['language'];    }
    if (isset($info['author']))      { $this->author      = $info['author'];      }
    if (isset($info['editor']))      { $this->editor      = $info['editor'];      }
    if (isset($info['webmaster']))   { $this->webmaster   = $info['webmaster'];   }
    // could probably be CLEANUP'd
  }
  
  // create a new entry
  // automatically called if a set_entry_* is called on a value that already exists on the current entry.
  public function new_entry () {
    $this->entrynr = $this->entrynr + 1;
    $this->entries[$this->entrynr] = array('title' => '', 'link' => '', 'description' => '', 'time' => 0, 'guid' => '');
  }
  
  // sets the title of the current entry
  public function set_entry_title ($title) {
    if ($this->entries[$this->entrynr]['title'] != '') {
      $this->new_entry();
    }
    $this->entries[$this->entrynr]['title'] = $title;
  }
  
  // sets the description of the current entry
  public function set_entry_description ($desc) {
    if ($this->entries[$this->entrynr]['description'] != '') {
      $this->new_entry();
    }
    $this->entries[$this->entrynr]['description'] = $desc;
  }

  // sets the relative link (not neccesarily permalink) to the current entry
  public function set_entry_link ($link) {
    if ($this->entries[$this->entrynr]['link'] != '') {
      $this->new_entry();
    }
    $this->entries[$this->entrynr]['link'] = $link;
  }
  
  // sets the publication time of the current entry
  // takes a UNIX timestamp
  public function set_entry_time ($time) {
    if ($this->entries[$this->entrynr]['time'] != '') {
      $this->new_entry();
    }
    $this->entries[$this->entrynr]['time'] = $time;
  }

  // sets the id (guid/uuid) of the current entry
  public function set_entry_id ($id) {
    if ($this->entries[$this->entrynr]['id'] != '') {
      $this->new_entry();
    }
    $this->entries[$this->entrynr]['id'] = $id;
  }
  
  // renders the rss
  public function render($format="rss") {
    // also add sorting by time here.
    require(dirname(__FILE__)."/templates/template-rss.php");
    $items = "";
    $arr_items = array_reverse($this->entries);
    preg_match_all("/\&\&\&([a-zA-Z-]*);/", $template['main'], $matches);
    foreach ($matches[0] as $value) {
      $key = trim($value, "&;");
      $key = strtolower($key);
      // add truly cool stuff here
    }
    return $this->rendered = $channel;
  }
  
  // forget everything. useful for generating multiple feeds.
  // experimental. may not work. at own risk. etc.
  public function purge() {
    $this = new RSS;
  }
}
?>
